/*
 * Entity.h
 *
 *  Created on: Mar 3, 2013
 *      Author: dennj
 */

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#ifndef ENTITY_H_
#define ENTITY_H_

class Entity
 {
	 int *DNA;
	 int health = 10; //Life points
 	 public:
	 	 Entity( void );
	 	 Entity( int newDNA[]); // Constructor
	 	 Entity( int length ); // Rnadom Entity
	 	 Entity( Entity c1, Entity c2 ); // CrossOver
	 	 ~Entity(){}; // Destructor
	 	 void SetDNA( int newDNA[]);
	 	 void Mutations( int rate );
	 	 int *GetDNA( void ); // Return DNA
 };

 Entity::Entity(  ) {
	 Entity( 20 );
 }

 Entity::Entity( int length ) {
	 int newDNA[length];
	 for( int i = 0; i < length; i++)
		 newDNA[i] = 0;
	 SetDNA(newDNA);
 }

 Entity::Entity( int newDNA[]) {
	 SetDNA(newDNA);
 }

 Entity::Entity( Entity c1, Entity c2 ) {
	 int *newDNA_1 = c1.GetDNA( ),
		 *newDNA_2 = c2.GetDNA( );

	 srand (time(NULL));
	 int x = 1 + rand() % ( sizeof( newDNA_1 ) -1 );	//breaking point of the dna
	 // x is random: 0 < x < length DNA
	 for(x; x <= sizeof( newDNA_2 ); x++)
		 newDNA_1[ x ] = newDNA_2[ x ];	//After breaking point, copy DNA_2 in DNA_1
	 SetDNA(newDNA_1);
 }

 void Entity::SetDNA( int newDNA []) {
	 DNA = newDNA;
 }

 void Entity::Mutations( int rate ){
	 srand (time(NULL));
	 int x;
	 for(int i = 0; i <= sizeof(DNA); i++){
		 x = rand()%rate;
		 switch(x){
		 	 case 0: DNA[i] = 0; break;
		 	 case 1: DNA[i] = 1; break;
		 }
	 }
 }

 int * Entity::GetDNA( void ) {
	 return DNA;
}

#endif /* ENTITY_H_ */
